#!/bin/bash
root_dir="/usr/local/edata"
if [ ! -d "$root_dir" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
  mkdir -p $root_dir
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
    exit 1
  fi
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi

jdk_ver=$(java -version 2>&1)
if [[ $jdk_ver == *"1.8.0_221"* ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO jdk 1.8.0_221 detected"
else
  if [[ $jdk_ver == *"not found"* ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING jdk is not found,installing..."
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR other version of jdk detected (1.8.0_221 is required),uninstall it first"
    exit 1
  fi
fi

if [ ! -d "$root_dir/hadoop" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/hadoop undetected"
  tar -xvf hadoop-3.3.4.tar.gz
  mv hadoop-3.3.4 $root_dir/hadoop
  mkdir -p $root_dir/hadoop/hdfs/data
  mkdir -p $root_dir/hadoop/hdfs/name
  mkdir -p $root_dir/hadoop/hdfs/journal
  mkdir -p $root_dir/hadoop/tmp
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir/hadoop detected"
fi

if [[ -z "$HADOOP_HOME" ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") HADOOP_HOME is not set,setting..."
  echo "export HADOOP_HOME=$root_dir/hadoop" >> ~/.bashrc
  echo 'export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH' >> ~/.bashrc
  echo "export HADOOP_HOME=$root_dir/hadoop" >> /etc/profile
  echo 'export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH' >> /etc/profile
  source ~/.bashrc
  source /etc/profile
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO HADOOP_HOME is set"
fi

mode=$1
if [[ -z "$mode" ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR hadoop mode is empty"
  exit 1
fi
echo $mode >> mode
env_file="hadoop_sa.env"
if [ $mode == "sa" ];then
  env_file="hadoop_sa.env"
fi

if [ $mode == "ha" ];then
  env_file="hadoop_ha.env"
fi

conf_dir=$root_dir/hadoop/etc/hadoop
while IFS= read -r line
do
  if [[ -n "$line" ]];then
    IFS="_" read -r prefix suffix <<< "$line"
    IFS="=" read -r name value <<< "$suffix"
    echo "$(date +"%Y-%m-%d %H:%M:%S") setting $name=$value"
    entry="<property><name>$name</name><value>$value</value></property>"
    entry_escaped=$(echo $entry | sed 's/\//\\\//g')
    if [ $prefix == "CORE" ];then
      sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/core-site.xml
    fi
    if [ $prefix == "HDFS" ];then
      sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/hdfs-site.xml
    fi
    if [ $prefix == "YARN" ];then
      sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/yarn-site.xml
    fi
    if [ $prefix == "MAPRED" ];then
      sed -i "/<\/configuration>/ s/.*/${entry_escaped}\n&/" $conf_dir/mapred-site.xml
    fi
    if [ $prefix == "WORKER" ];then
      rm $conf_dir/workers
      echo -e "$value" >> $conf_dir/workers
    fi
  fi
done < "$env_file"
hdfs version
if [  $? -eq 0 ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO hadoop setup success"
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR hadoop setup error"
fi




